/*********************
 *      INCLUDES
 *********************/

 #include <rtthread.h>
 #include <rtdevice.h>
 #include "littlevgl2rtt.h"
 #include "lvgl.h"
 #include "lvsf_comp.h"
 #include "gui_app_fwk.h"
 #include "lv_ext_resource_manager.h"
 #include "lv_ex_data.h"
 #include "app_mem.h"
 #include "max30102.h"  // 包含传感器头文件
 
 #define I2C_BUS_NAME "i2c1"  // 根据实际硬件修改I2C总线名称
 #define SENSOR_UPDATE_INTERVAL 100  // 传感器更新间隔(ms)
 
 static lv_obj_t *hr_label;    // 心率显示标签
 static lv_obj_t *spo2_label;  // 血氧显示标签
 static lv_obj_t *temp_label;  // 温度显示标签
 static rt_timer_t sensor_timer;  // 传感器读取定时器
 static lv_timer_t *update_timer = RT_NULL;  // LVGL更新定时器
 
 static max30102_data_t sensor_data;
 static max30102_result_t sensor_result;
 
 // 更新显示
 static void update_display(void)
 {
     char buf[32];
     
     // 更新心率显示
     rt_sprintf(buf, "心率: %d BPM", sensor_result.heart_rate);
     lv_label_set_text(hr_label, buf);
     
     // 更新血氧显示
     rt_sprintf(buf, "血氧: %.1f%%", sensor_result.spo2);
     lv_label_set_text(spo2_label, buf);
     
     // 更新温度显示
     rt_sprintf(buf, "温度: %.1f°C", sensor_result.temperature);
     lv_label_set_text(temp_label, buf);
 }
 
 // LVGL定时器回调
 static void update_timer_cb(lv_timer_t *timer)
 {
     update_display();
 }
 
 // 传感器数据更新回调
 static void sensor_update(void *parameter)
 {
     rt_err_t ret;
     
     // 读取传感器原始数据
     ret = max30102_read_data(&sensor_data);
     if (ret != RT_EOK)
     {
         rt_kprintf("读取传感器数据失败\n");
         return;
     }
     
     // 计算心率和血氧
     max30102_calculate(&sensor_data, &sensor_result);
 }
 
 static void on_start(void)
 {
     // 创建主容器
     lv_obj_t *lc = lv_obj_create(lv_scr_act());
     lv_obj_set_size(lc, LV_HOR_RES_MAX, LV_VER_RES_MAX);
     lv_obj_align(lc, LV_ALIGN_CENTER, 0, 0);
 
     // 创建标题
     lv_obj_t *title = lv_label_create(lc);
     lv_label_set_text(title, "心率血氧检测");
     lv_ext_set_local_font(title, FONT_BIGL, LV_COLOR_WHITE);
     lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
 
     // 创建心率显示标签
     hr_label = lv_label_create(lc);
     lv_ext_set_local_font(hr_label, FONT_BIGL, LV_COLOR_WHITE);
     lv_obj_align(hr_label, LV_ALIGN_CENTER, 0, -40);
 
     // 创建血氧显示标签
     spo2_label = lv_label_create(lc);
     lv_ext_set_local_font(spo2_label, FONT_BIGL, LV_COLOR_WHITE);
     lv_obj_align(spo2_label, LV_ALIGN_CENTER, 0, 0);
 
     // 创建温度显示标签
     temp_label = lv_label_create(lc);
     lv_ext_set_local_font(temp_label, FONT_BIGL, LV_COLOR_WHITE);
     lv_obj_align(temp_label, LV_ALIGN_CENTER, 0, 40);
 
     // 初始化传感器
     if (rt_hw_max30102_init(I2C_BUS_NAME) != RT_EOK)
     {
         lv_label_set_text(hr_label, "传感器初始化失败");
         lv_label_set_text(spo2_label, "请检查硬件");
         lv_label_set_text(temp_label, "");
         return;
     }
 
     // 初始化传感器结果
     sensor_result.heart_rate = 0;
     sensor_result.spo2 = 0.0f;
     sensor_result.temperature = 0.0f;
 
     // 创建传感器读取定时器
     sensor_timer = rt_timer_create("max30102_timer", 
                                   sensor_update, 
                                   RT_NULL, 
                                   SENSOR_UPDATE_INTERVAL, 
                                   RT_TIMER_FLAG_PERIODIC);
     if (sensor_timer != RT_NULL)
     {
         rt_timer_start(sensor_timer);
     }
     else
     {
         lv_label_set_text(hr_label, "定时器创建失败");
         lv_label_set_text(spo2_label, "系统错误");
         lv_label_set_text(temp_label, "");
         return;
     }
 
     // 创建LVGL定时器用于更新显示
     update_timer = lv_timer_create(update_timer_cb, 500, NULL);  // 500ms更新一次
 
     // 初始更新显示
     update_display();
 }
 
 static void on_pause(void)
 {
     // 暂停时停止定时器
     if (sensor_timer)
         rt_timer_stop(sensor_timer);
     if (update_timer)
         lv_timer_pause(update_timer);
 }
 
 static void on_resume(void)
 {
     // 恢复时启动定时器
     if (sensor_timer)
         rt_timer_start(sensor_timer);
     if (update_timer)
         lv_timer_resume(update_timer);
 }
 
 static void on_stop(void)
 {
     // 停止时删除定时器
     if (sensor_timer)
     {
         rt_timer_delete(sensor_timer);
         sensor_timer = RT_NULL;
     }
     if (update_timer)
     {
         lv_timer_del(update_timer);
         update_timer = RT_NULL;
     }
 }
 
 static void msg_handler(gui_app_msg_type_t msg, void *param)
 {
     switch (msg)
     {
     case GUI_APP_MSG_ONSTART:
         on_start();
         break;
     case GUI_APP_MSG_ONRESUME:
         on_resume();
         break;
     case GUI_APP_MSG_ONPAUSE:
         on_pause();
         break;
     case GUI_APP_MSG_ONSTOP:
         on_stop();
         break;
     default:
         break;
     }
 }
 
 // 声明所需图像资源
 LV_IMG_DECLARE(img_red_heart);
 #define SPO2_HEART_RATE_APP_ID "hr_app"
 static int app_main(intent_t i)
 {
     gui_app_regist_msg_handler(SPO2_HEART_RATE_APP_ID, msg_handler);
     return 0;
 }
 
 // 导出应用
 BUILTIN_APP_EXPORT(LV_EXT_STR_ID(heart_demos), LV_EXT_IMG_GET(img_red_heart), SPO2_HEART_RATE_APP_ID, app_main);